﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Zen.Barcode;
using System.Reflection;
using Worktool.CODE_Chris.SQL_DATA;
using Worktool.CODE_Chris.DataSourceHelpers;

namespace Worktool
{
    public partial class DmxDevices : Form
    {
        private String pkInstalledPrinters;
        private PrintDocument label;

        private SQL sql;
        private Boolean isInit = true;

        private DmxDevicesHelper helper;
        
        public DmxDevices()
        {
            InitializeComponent();
            this.Text = "DMX Devices: " + Login.currentDBName; 
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new DmxDevicesHelper(dataGridView1, this);

            sql.sendSQL("dbo.updatedmxDevices");

            fillElementsInit();

            fillTablesInit();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            // Drucker in das Auswahlfeld eintragen
            comboBox3.Items.Clear();
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                comboBox3.Items.Add(pkInstalledPrinters);
            }
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                if (Login.labelPrinterName == PrinterSettings.InstalledPrinters[i])
                {
                    comboBox3.Text = Login.labelPrinterName;
                }
            }
        }

        private void DmxDevices_Load(object sender, EventArgs e)
        {
            typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
        }
        #endregion

        #region Export
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }
        #endregion

        #region Filter
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
        }
        #endregion

        #region Drucken
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count != 0)
                {

                    // Zugehöriges Label ausdrucken
                    label = new PrintDocument();

                    if (comboBox3.Text != "TEC B-EV4-T" && comboBox3.Text != "TEC B-EV4-G")
                    {
                        PaperSize pS = new PaperSize("Custom Size", 620, 620);
                        label.DefaultPageSettings.PaperSize = pS;
                        label.PrinterSettings.PrinterName = comboBox3.Text;
                        label.PrinterSettings.DefaultPageSettings.PaperSize = pS;
                        label.PrintPage += new PrintPageEventHandler(label_PrintPage);
                        label.Print();
                    }
                    else
                    {
                        //Labeldrucker mit Silber-Labels
                        label.PrinterSettings.PrinterName = comboBox3.Text;
                        label.PrintPage += new PrintPageEventHandler(label_PrintPage);
                        label.Print();
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Fehler in button3_Click:\r\n\r\nFehlermeldung:\r\n" + ex.StackTrace.ToString());
            }

        }

        private void label_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                if (comboBox3.Text != "TEC B-EV4-T" && comboBox3.Text != "TEC B-EV4-G")
                {
                    SolidBrush brush = new SolidBrush(Color.Black);
                    System.Drawing.Font header = new System.Drawing.Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Bold);
                    Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
                    Image img;

                    e.Graphics.DrawString("Venue: " + dataGridView1.SelectedRows[0].Cells["Venue"].Value.ToString(), header, brush, 5, 5);
                    e.Graphics.DrawString("Manufacturer: " + dataGridView1.SelectedRows[0].Cells["Manufacturer"].Value.ToString(), header, brush, 5, 17);
                    e.Graphics.DrawString("Model: " + dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(), header, brush, 5, 29);
                    e.Graphics.DrawString("SN: " + dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString(), header, brush, 5, 41);
                    e.Graphics.DrawString("Position: " + dataGridView1.SelectedRows[0].Cells["Positions_ID"].Value.ToString(), header, brush, 5, 53);
                    e.Graphics.DrawString("DMX: " + dataGridView1.SelectedRows[0].Cells["dmxAdress"].Value.ToString(), header, brush, 5, 65);
                    e.Graphics.DrawString("Universe: " + dataGridView1.SelectedRows[0].Cells["universe"].Value.ToString(), header, brush, 5, 77);
                    e.Graphics.DrawString("Fixture: " + dataGridView1.SelectedRows[0].Cells["fixture"].Value.ToString(), header, brush, 5, 89);

                    //  Upcoming for fixturenumber e.Graphics.DrawString("Model: " + dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(), header, brush, 5, 5);

                    // Logo
                    e.Graphics.DrawImage(Worktool.Properties.Resources.ASC_Logo_quadratisch, 200, 10, 30, 30);
                    // Barcode
                    if (dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString() != "")
                    {
                        img = barcode128.Draw(dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString(), 30);
                        e.Graphics.DrawImage(img, 5, 113);
                    }
                }
                else
                {
                    //Labeldrucker mit Silber-Labels
                    SolidBrush brush = new SolidBrush(Color.Black);
                    System.Drawing.Font header = new System.Drawing.Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Bold);
                    Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
                    Image img;

                    e.Graphics.DrawString("Venue: " + dataGridView1.SelectedRows[0].Cells["Venue"].Value.ToString(), header, brush, 5, 25);
                    e.Graphics.DrawString("Manufacturer: " + dataGridView1.SelectedRows[0].Cells["Manufacturer"].Value.ToString(), header, brush, 5, 37);
                    e.Graphics.DrawString("Model: " + dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(), header, brush, 5, 49);
                    e.Graphics.DrawString("SN: " + dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString(), header, brush, 5, 61);
                    e.Graphics.DrawString("Position: " + dataGridView1.SelectedRows[0].Cells["Positions_ID"].Value.ToString(), header, brush, 5, 73);
                    e.Graphics.DrawString("DMX: " + dataGridView1.SelectedRows[0].Cells["dmxAdress"].Value.ToString(), header, brush, 5, 85);
                    e.Graphics.DrawString("Universe: " + dataGridView1.SelectedRows[0].Cells["universe"].Value.ToString(), header, brush, 5, 97);
                    e.Graphics.DrawString("Fixture: " + dataGridView1.SelectedRows[0].Cells["fixture"].Value.ToString(), header, brush, 5, 109);

                    // Logo
                    e.Graphics.DrawImage(Worktool.Properties.Resources.ASC_Logo_quadratisch, 250, 0, 40, 40);
                    // Barcode
                    if (dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString() != "")
                    {
                        img = barcode128.Draw(dataGridView1.SelectedRows[0].Cells["Seriennummer"].Value.ToString(), 40);
                        e.Graphics.DrawImage(img, 6, 148);
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Fehler in label_PrintPage:\r\n\r\nFehlermeldung:\r\n" + ex.StackTrace.ToString());
            }
        }
        #endregion

        #region Daten neu laden
        public void reloadAllData()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            fillElementsInit();
            fillTablesInit();
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }
        #endregion
    }
}
